package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.UserInfoHolder;
import com.wbao.dianniu.adapter.WorkExperienceAdapter;
import com.wbao.dianniu.customView.MyListView;
import com.wbao.dianniu.data.JobDetailData;
import com.wbao.dianniu.data.WorkExperienceData;
import com.wbao.dianniu.listener.IJobDetailListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobDetailManager;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 简历预览
 *
 * @author yejianjun
 */
public class JobPreviewActivity extends BaseActivity implements IJobDetailListener {

    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;

    //    private ImageView iv_head;
//    private TextView tv_name;
//    private TextView tv_label;
//    private ImageView sexIV;
//    private ImageView addV;
    private TextView tv_post;
    private TextView tv_salary;
    private TextView tv_address;
    private TextView tv_experience;
    private TextView tv_education;
    private MyListView mListView;
    private JobDetailData data;
    private JobDetailManager manager;
    private boolean isModifyPreview = true;
    private Button rightBtn;
    private WorkExperienceAdapter adapter;
    private List<WorkExperienceData> experienceList;
    private UserInfoHolder holder;
    private ScrollView mScrollView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.job_preview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.resume_preview));
        initWattingView();
        init();
        initListData();
        initData();
        if (!isModifyPreview) {
            manager = new JobDetailManager(this);
            manager.addJobDetailListener(this);
            manager.jobDetail(GlobalContext.getAccountId(), GlobalContext.getAccountId());
        } else {
            waittingLayout.setVisibility(View.GONE);
            rightBtn.setVisibility(View.INVISIBLE);
        }
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(Const.BUNDLE_DATA);
        if (null != bundle) {
            PictureDownloadUtils.displayHeadImage(this, holder.headIV, bundle.getString(Const.BUNDLE_HEADPIC, ""));
            holder.nameTV.setText(bundle.getString(Const.BUNDLE_REALNAME, ""));
            holder.labelTV.setText(bundle.getString(Const.BUNDLE_LABEL, ""));
            Utils.showSex(this, bundle.getInt(Const.INTENT_SEX), holder.sexIV);
            if (bundle.getBoolean(Const.INTENT_IS_MESSAGE)) {
                holder.addV.setVisibility(View.VISIBLE);
            } else {
                holder.addV.setVisibility(View.GONE);
            }
            tv_post.setText(bundle.getString(Const.BUNDLE_SUBJECT, ""));
            tv_salary.setText(bundle.getString(Const.BUNDLE_SALARY, ""));
            tv_address.setText(bundle.getString(Const.BUNDLE_ADDRESS, ""));
            tv_experience.setText(bundle.getString(Const.BUNDLE_EXPERIENCE, ""));
            tv_education.setText(bundle.getString(Const.BUNDLE_EDUCATION, ""));
            String describe = bundle.getString(Const.BUNDLE_CONTENT, "");
            if (!TextUtils.isEmpty(describe)) {
                experienceList = Utils.parseWorkData(describe);
                adapter.setData(experienceList);
                setListViewHeightBasedOnChildren(mListView);
                adapter.notifyDataSetChanged();
            }
        } else {
            isModifyPreview = false;
        }
    }

    private void init() {
        holder = new UserInfoHolder();
        holder.headIV = findViewById(R.id.user_head);
        holder.nameTV = findViewById(R.id.user_name);
        holder.labelTV = findViewById(R.id.user_label);
        holder.sexIV = findViewById(R.id.user_sex);
        holder.partnerIV = findViewById(R.id.user_partner);
        holder.addV = findViewById(R.id.add_v_iv);
        tv_post = findViewById(R.id.job_preview_post);
        tv_salary = findViewById(R.id.job_preview_money);
        tv_address = findViewById(R.id.job_preview_place);
        tv_experience = findViewById(R.id.job_preview_years);
        tv_education = findViewById(R.id.job_preview_education);
        mListView = findViewById(R.id.job_preview_describle);
        mScrollView = findViewById(R.id.job_preview_scroll);
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.share));
        rightBtn.setOnClickListener(this);
    }

    private void initListData() {
        experienceList = new ArrayList<WorkExperienceData>();
        adapter = new WorkExperienceAdapter(this, 1);
        mListView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(mListView);
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_button:
                // 调用分享内容
                String imageUrl;
                if (TextUtils.isEmpty(data.getHeadPic())) {
                    imageUrl = GlobalContext.cdndownUrl + "logo.png";
                } else {
                    imageUrl = GlobalContext.cdndownUrl + data.getHeadPic();
                }
                String content = "应聘岗位：" + data.getPost() + "\n" + "工作经验：" + data.getExperience();
                ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                shareSDKManager.showShare(this, content, 4, imageUrl, data.getId());
                break;
            default:
                break;
        }
    }

    @Override
    public void onJobDetailFailure(int errorCode, String errorMsg) {
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onJobDetailSuccess(JobDetailData data) {
        if (null == data) {
            waittingLayout.setVisibility(View.VISIBLE);
            waittingProBar.setVisibility(View.GONE);
            waittingTV.setText("数据为空...");
            return;
        }
        waittingLayout.setVisibility(View.GONE);
        this.data = data;
        display(data);
        mScrollView.post(new Runnable() {
            @Override
            public void run() {
                mScrollView.smoothScrollTo(0, 10);
            }
        });

    }

    private void display(JobDetailData data) {
        if (!TextUtils.isEmpty(data.getHeadPic())) {
            PictureDownloadUtils.displayHeadImage(this, holder.headIV, data.getHeadPic());
        }
        holder.nameTV.setText(data.getRealName() == null ? "" : data.getRealName());
        holder.labelTV.setText(data.getLabel() == null ? "" : data.getLabel());
        Utils.showSex(this, data.getUserInfo().sex, holder.sexIV);
        Utils.showPartner(this, data.getUserInfo().partner, holder.partnerIV);
        Utils.showAuth(data.getUserInfo(), holder.addV);
        tv_post.setText(data.getPost() == null ? "" : data.getPost());
        tv_salary.setText(data.getSalary() == null ? "" : data.getSalary());
        tv_address.setText(data.getAddress() == null ? "" : data.getAddress());
        tv_experience.setText(data.getExperience() == null ? "" : data.getExperience());
        tv_education.setText(data.getEducation() == null ? "" : data.getEducation());
        if (!TextUtils.isEmpty(data.getDescribe())) {
            experienceList = Utils.parseWorkData(data.getDescribe());
            adapter.setData(experienceList);
            setListViewHeightBasedOnChildren(mListView);
            adapter.notifyDataSetChanged();
        }
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter   
        int totalHeight = 0;
        for (int i = 0, len = adapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目   
            View listItem = adapter.getView(i, null, listView);
            // 计算子项View 的宽高   
            listItem.measure(0, 0);
            // 统计所有子项的总高度   
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度   
        // params.height最后得到整个ListView完整显示需要的高度   
        listView.setLayoutParams(params);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeJobDetailListener(this);
        }
    }
}
